package com.flash.DayProcessing.controller;


import com.flash.BusinessData.entity.CashAccrued;
import com.flash.BusinessData.entity.securityAccrued;
import com.flash.BusinessData.service.CashAccruedService;
import com.flash.BusinessData.service.SecurityAccruedService;
import com.flash.CashAdministration.entity.FundsAllot;
import com.flash.CashAdministration.service.FundsAllotService;
import com.flash.DayProcessing.entity.IncomePayments;
import com.flash.DayProcessing.service.IncomePaymentsService;
import com.flash.GlobalManagement.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class IncomePaymentsController {

    @Autowired
    IncomePaymentsService incomePaymentsService;

    @Autowired
    CashAccruedService cashAccruedService;

    @Autowired
    SecurityAccruedService securityAccruedService;

    @Autowired
    FundsAllotService fundsAllotService;

    @Resource
    Dbutil dbutil;
    @RequestMapping("selectCashInterestIncome")
    @ResponseBody
    public Map<String, Object> selectCashInterestIncome(int page, int limit, String statDate,HttpServletRequest request) {
        HttpSession session = request.getSession();
        List<IncomePayments> incomePayments = incomePaymentsService.selectCashInterestIncome(page, limit, statDate, (String) session.getAttribute("fundId"));
        System.out.println("statDate = " + statDate);
        Map<String, Object> json = new HashMap<>();
        System.out.println(page);
        System.out.println(limit);
        System.out.println(incomePayments);
        json.put("code",0);
        json.put("msg","");
        json.put("count",0);
        json.put("data",incomePayments);
        json.put("date",statDate);
        System.out.println(json);
        return json;
    }

    @RequestMapping("selectBondInterestIncome")
    @ResponseBody
    public Map<String, Object> selectBondInterestIncome(int page, int limit, String statDate, HttpServletRequest request) {
        HttpSession session = request.getSession();
        List<IncomePayments> incomePayments = incomePaymentsService.selectBondInterestIncome(page, limit, statDate, (String) session.getAttribute("fundId"));
        Map<String, Object> json = new HashMap<>();
        System.out.println(page);
        System.out.println(limit);
        System.out.println(incomePayments);
        json.put("code",0);
        json.put("msg","");
        json.put("count",0);
        json.put("data",incomePayments);
        json.put("date",statDate);
        System.out.println(json);
        return json;
    }

    @RequestMapping("selectPayTwoFees")
    public Map<String, Object> selectPayTwoFees(int page, int limit, String statDate, HttpServletRequest request){
        HttpSession session = request.getSession();
        List<IncomePayments> incomePayments = incomePaymentsService.selectPayTwoFees(page, limit, statDate, (String) (session.getAttribute("fundId")));
        Map<String, Object> json = new HashMap<>();
        System.out.println(page);
        System.out.println(limit);
        System.out.println(incomePayments);
        json.put("code",0);
        json.put("msg","");
        json.put("count",0);
        json.put("data",incomePayments);
        json.put("date",statDate);
        System.out.println(json);
        return json;
    }
    @RequestMapping("statisticCas")
    public int statisticCas(String cash , HttpServletRequest request) {
        System.out.println("进入了统计现金Controller...");
        System.out.println(cash);
        int i=0;
        HttpSession session = request.getSession();
        List<IncomePayments> incomePayments = SysUtil.jsonToArrayList(cash,IncomePayments.class);
        CashAccrued cashAccrued = new CashAccrued();
        FundsAllot fundsAllot = new FundsAllot();
        for (IncomePayments incomePayments1:incomePayments) {
           cashAccrued.setBusinessTime(incomePayments1.getDateAccrualStock());
           cashAccrued.setBusinessType(incomePayments1.getBusinessType());
           cashAccrued.setMoney(incomePayments1.getMoney());
           cashAccrued.setFundFlow(incomePayments1.getBusinessState());
           cashAccrued.setFundId((String) session.getAttribute("fundId"));
           cashAccrued.setCashId(incomePayments1.getCashId());
           cashAccrued.setCashAccruedId(incomePayments1.getCashAccrualStockId());
            cashAccruedService.deleteCashAccrueNews(cashAccrued);
            System.out.println((String) session.getAttribute("fundId"));


            cashAccrued.setCashAccruedId(dbutil.getMaxId(TableIdUtil.CAID, TableNameUtil.CA, TableTimeUtil.CATime));
            cashAccrued.setBusinessTime(incomePayments1.getDateAccrualStock());
            cashAccrued.setBusinessType(incomePayments1.getBusinessType());
            cashAccrued.setMoney(incomePayments1.getMoney());
            cashAccrued.setFundFlow(incomePayments1.getBusinessState()*-1);
            cashAccrued.setFundId((String) session.getAttribute("fundId"));
            cashAccrued.setCashId(incomePayments1.getCashId());
            cashAccrued.setCashAccruedId(incomePayments1.getCashAccrualStockId());
            i=cashAccruedService.insertCashAccrued(cashAccrued);
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
            Date date=new Date();
            String statDate=simpleDateFormat.format(date);
            fundsAllot.setFundsAllotId(NumberUtil.getNumber(TableIdUtil.FAID, TableNameUtil.FA,"FA"));
            fundsAllot.setFundId((String) session.getAttribute("fundId"));
            fundsAllot.setCashId(incomePayments1.getCashId());
            fundsAllot.setAllotDirection(incomePayments1.getBusinessState());
            fundsAllot.setAllotMoney(incomePayments1.getMoney());
            fundsAllot.setBusinessTime(incomePayments1.getDateAccrualStock());
            fundsAllot.setBusinessId("123");
            fundsAllot.setAllotTime(incomePayments1.getDateAccrualStock());
            fundsAllot.setAllotRemark("666666666");
            int allocatingType=0;
            //判断cashInterestIncome.getBusinessType() 类型为3的话allocatingType=1 为1、2的话allocatingType=6
            if(incomePayments1.getBusinessType()==3){
                allocatingType=1;
            }
            fundsAllot.setAllotType(allocatingType);
            System.out.println(fundsAllot);
            fundsAllotService.insertFundsAllot(fundsAllot);
        }
        return i;
    }
    @RequestMapping("statisticSecurties")
    public int statisticSecurties(String Securities,HttpServletRequest request) {
        System.out.println("进入债卷的Controller...");
        System.out.println(Securities);
        int i=0;
        HttpSession session = request.getSession();
        List<IncomePayments> BondInterestIncome = SysUtil.jsonToArrayList(Securities,IncomePayments.class);
        FundsAllot fundsAllot = new FundsAllot();
        securityAccrued securityAccrued = new securityAccrued();
        for (IncomePayments bondInterestIncome:BondInterestIncome) {
            securityAccrued.setBusinessTime(bondInterestIncome.getDateAccrualStock());
            securityAccrued.setBusinessType(bondInterestIncome.getSecurityType());
            securityAccruedService.SecurityAccruedDeletes(securityAccrued);

            securityAccrued.setSecurityAccruedId(dbutil.getMaxId(TableIdUtil.SAID, TableNameUtil.SA, TableTimeUtil.SATime));
            securityAccrued.setBusinessTime(bondInterestIncome.getDateAccrualStock());
            securityAccrued.setBusinessType(bondInterestIncome.getSecurityType());
            securityAccrued.setCashId("C20220622001");
            securityAccrued.setFundId((String) session.getAttribute("fundId"));
            securityAccrued.setFundFlow(bondInterestIncome.getBusinessState()*-1);
            securityAccrued.setMoney(bondInterestIncome.getMoney());
            securityAccrued.setSecurityId("624415");
            i=securityAccruedService.SecurityAccruedInsert(securityAccrued);
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
            Date date=new Date();
            String statDate=simpleDateFormat.format(date);
            fundsAllot.setFundsAllotId(NumberUtil.getNumber(TableIdUtil.FAID, TableNameUtil.FA,"FA"));
            fundsAllot.setFundId((String) session.getAttribute("fundId"));
            fundsAllot.setCashId((String) session.getAttribute("cashId"));
            fundsAllot.setAllotDirection(bondInterestIncome.getSecurityType());
            fundsAllot.setAllotMoney(bondInterestIncome.getMoney());
            fundsAllot.setBusinessTime(bondInterestIncome.getDateAccrualStock());
            fundsAllot.setBusinessId("123");
            fundsAllot.setAllotTime(bondInterestIncome.getDateAccrualStock());
            fundsAllot.setAllotRemark("666666666");
            int allocatingType=0;
            //判断cashInterestIncome.getBusinessType() 类型为3的话allocatingType=1 为1、2的话allocatingType=6
            if(securityAccrued.getBusinessType()==3){
                allocatingType=4;
            }
            fundsAllot.setAllotType(allocatingType);
            System.out.println(fundsAllot);
            fundsAllotService.insertFundsAllot(fundsAllot);
        }
    return i;
    }
    @RequestMapping("statisticPay")
    public int statisticPay(String TwoFees , HttpServletRequest request) {
        System.out.println("进入了两费Controller...");
        System.out.println(TwoFees);
        int i=0;
        HttpSession session = request.getSession();
        List<IncomePayments> incomePayments = SysUtil.jsonToArrayList(TwoFees,IncomePayments.class);
        CashAccrued cashAccrued = new CashAccrued();
        FundsAllot fundsAllot = new FundsAllot();
        for (IncomePayments incomePayments1:incomePayments) {
            cashAccrued.setBusinessTime(incomePayments1.getDateAccrualStock());
            cashAccrued.setBusinessType(incomePayments1.getBusinessType());
            cashAccrued.setMoney(incomePayments1.getMoney());
            cashAccrued.setFundFlow(incomePayments1.getBusinessState());
            cashAccrued.setFundId((String) session.getAttribute("fundId"));
            cashAccrued.setCashId(incomePayments1.getCashId());
            cashAccrued.setCashAccruedId(incomePayments1.getCashAccrualStockId());
            cashAccruedService.deleteCashAccrueNews(cashAccrued);
            System.out.println((String) session.getAttribute("fundId"));


            cashAccrued.setCashAccruedId(NumberUtil.getNumber(TableIdUtil.CAID, TableNameUtil.CA,"CA"));

            cashAccrued.setBusinessTime(incomePayments1.getDateAccrualStock());
            cashAccrued.setBusinessType(incomePayments1.getBusinessType());
            cashAccrued.setMoney(incomePayments1.getMoney());
            cashAccrued.setFundFlow(incomePayments1.getBusinessState()*-1);
            cashAccrued.setFundId((String) session.getAttribute("fundId"));
            cashAccrued.setCashId(incomePayments1.getCashId());
            cashAccrued.setCashAccruedId(incomePayments1.getCashAccrualStockId());
            i=cashAccruedService.insertCashAccrued(cashAccrued);
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
            Date date=new Date();
            String statDate=simpleDateFormat.format(date);
            fundsAllot.setFundsAllotId(NumberUtil.getNumber(TableIdUtil.FAID, TableNameUtil.FA,"FA"));
            fundsAllot.setFundId((String) session.getAttribute("fundId"));
            fundsAllot.setCashId(incomePayments1.getCashId());
            fundsAllot.setAllotDirection(incomePayments1.getBusinessState());
            fundsAllot.setAllotMoney(incomePayments1.getMoney());
            fundsAllot.setBusinessTime(incomePayments1.getDateAccrualStock());
            fundsAllot.setBusinessId("123");
            fundsAllot.setAllotTime(incomePayments1.getDateAccrualStock());
            fundsAllot.setAllotRemark("666666666");
            int allocatingType=0;
            //判断cashInterestIncome.getBusinessType() 类型为3的话allocatingType=1 为1、2的话allocatingType=6
            if(incomePayments1.getBusinessType()==3){
                allocatingType=1;
            }
            fundsAllot.setAllotType(allocatingType);
            System.out.println(fundsAllot);
            fundsAllotService.insertFundsAllot(fundsAllot);
        }
        return i;
    }
}
